package com.wedata.海康云;

import cn.hutool.core.date.DatePattern;
import cn.hutool.json.JSONObject;
import com.disdr.utils.DateUtils;
import com.wedata.HttpUtils;

import java.util.*;

/**
 * 直接查设备上的人
 * https://open.hikyun.com/1/document/827
 */
public class 门禁设备接入 extends BaseService{



    public static void main(String[] args) {
        //refresh();
        search();
        //delete();
        //add();
    }

    /**
     * 搜索：查询设备上的人
     */
    public static void search() {
        JSONObject param = new JSONObject();
        param.set("projectId",projectId);
        //param.set("productCode",productCode);
        param.set("deviceSerial","FD6069439");

        param.set("searchID", "gymcityDataCenter");
        param.set("searchResultPosition", 0);
        param.set("maxResults", 2);

        /*List<Map> list = new ArrayList<>();
        Map<String,String> employeeNo = new HashMap<>();
        employeeNo.put("employeeNo", "563254210931144");
        list.add(employeeNo);
        param.set("employeeNoList", list);*/

        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v1/accesscontrol/userinfo/search/post",header,param);
        System.out.println(jsonObject);
        //{"msg":"操作成功","code":"200","data":{"userInfo":[{"doorRight":"1","roomNumber":1,"gender":"unknown","numOfCard":0,"employeeNo":"563254210931144","closeDelayEnabled":false,"valid":{"timeType":"local","enable":true,"endTime":"2037-12-31T23:59:59","beginTime":"2000-01-01T00:00:00"},"rightPlan":[{"planTemplateNo":"7","doorNo":1}],"password":"","belongGroup":"","openDoorTime":0,"maxOpenDoorTime":0,"floorNumber":1,"name":"蓝斌","localUIRight":false,"userType":"normal","personInfoExtends":[{"value":"2438"}],"numOfFace":1}],"responseStatusStrg":"OK","searchID":"gymcityDataCenter","numOfMatches":1,"totalMatches":1}}
        //{"msg":"操作成功","code":"200","data":{"responseStatusStrg":"NO MATCH","searchID":"gymcityDataCenter","numOfMatches":0,"totalMatches":0}}
        //{"msg":"Device off line","code":"20007","data":null}
        if("200".equals(jsonObject.getStr("code"))){

        }
    }
    /**
     * 删除：删除设备上的人
     */
    public static void delete() {
        JSONObject param = new JSONObject();
        param.set("projectId",projectId);
        //param.set("productCode",productCode);
        param.set("deviceSerial","AE6891584");
        //{"msg":"deviceSerial不能为空","code":"400","data":null}

       List<Map> list = new ArrayList<>();
        for(int i = 1; i<15; i ++){
            Map<String,String> employeeNo = new HashMap<>();
            employeeNo.put("employeeNo", ""+i);
            list.add(employeeNo);
        }
        //param.set("employeeNoList", list);

        System.out.println(param);
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject1 = HttpUtils.sendPut("https://open.hikyun.com/artemis/api/eits/v1/accesscontrol/userinfo/delete/put",header,param);
        System.out.println(jsonObject1);
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v1/accesscontrol/userinfo/delete/put",header,param);
        System.out.println(jsonObject);
    }
    /**
     * 添加:这个直接加的人，不会把人脸信息下发，这个只能用账号密码登录，没用
     */
    public static void add() {
        JSONObject param = new JSONObject();
        param.set("projectId",projectId);
        //param.set("productCode",productCode);
        param.set("deviceSerial","AE6891584");

        param.set("employeeNo", "2164919224434096");
        param.set("name","哈哈");
        param.set("userType", "normal");

        param.set("floorNumber",1);
        param.set("roomNumber",1);

        Map<String,Object> map = new HashMap<>();
        map.put("enable",true);
        map.put("beginTime", DateUtils.parseDateToStr(DatePattern.UTC_SIMPLE_PATTERN, new Date()));
        map.put("endTime", DateUtils.parseDateToStr(DatePattern.UTC_SIMPLE_PATTERN, new Date(System.currentTimeMillis()+86400000)));
        map.put("timeType","local");
        param.set("valid", map);

        System.out.println(param);
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        //https://open.hikyun.com/artemis/api/eits/v1/accesscontrol/userinfo/record/post
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v2/accesscontrol/userinfo/record/post",header,param);
        System.out.println(jsonObject);
    }

    //重新下发人员权限--这个好像是根据权限组来的
    //https://open.hikyun.com/1/document/432#996
    public static void refresh() {
        JSONObject param = new JSONObject();
        param.set("projectId",projectId);
        param.set("productCode",productCode);

        List<Map> personDeviceList = new ArrayList<>();
        Map<String,String> employeeNo1 = new HashMap<>();
        employeeNo1.put("personId", "563254210931144");
        employeeNo1.put("deviceSerial", "AE6891584");
        personDeviceList.add(employeeNo1);
        Map<String,String> employeeNo2 = new HashMap<>();
        employeeNo2.put("personId", "2164919224434096");
        employeeNo2.put("deviceSerial", "AE6891584");
        personDeviceList.add(employeeNo2);
        param.set("personDeviceList", personDeviceList);


        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        //https://open.hikyun.com/artemis/api/eits/v2/accesscontrol/userinfo/record/post
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/acpmcs/v1/auth/download/device/person/refresh",header,param);
        System.out.println(jsonObject);
    }

    //saas接口的权限不一样
    private static void 删除设备上的人员(){
        //https://open.hikyun.com/1/document/309#17
        JSONObject param = new JSONObject();
        param.set("projectId",projectId);
        param.set("personId",1848414285032L);//不指定人员就是删除所有人员
        param.set("deviceSerial","AE6891584");
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v1/saas/acs/person/delete",header,param);
        System.out.println(jsonObject);
        //{"msg":"指定设备权限不存在","code":"saas-71002","data":null}

    }
    //saas接口的权限不一样
    private static void 下发人员信息到门禁(){
        //https://open.hikyun.com/1/document/309#16
        JSONObject param = new JSONObject();
        param.set("projectId",projectId);
        param.set("personId",1848414285032L);
        param.set("deviceSerial","AE6891584");
        param.set("isSetFace",1);
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v1/saas/acs/person/add",header,param);
        System.out.println(jsonObject);
        //{"msg":"网络异常或系统繁忙","code":"saas-8888","data":null}
    }

}

